package com.lh.blogTrivia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.blogCommon.result.LhR;
import com.lh.blogCommon.util.PageUtils;
import com.lh.blogCommon.util.Query;
import com.lh.blogTrivia.entity.PhotoAlbumEntity;
import com.lh.blogTrivia.feign.OssFeignService;
import com.lh.blogTrivia.mapper.PhotoAlbumMapper;
import com.lh.blogTrivia.service.PhotoAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("photoAlbumService")
public class PhotoAlbumServiceImpl extends ServiceImpl<PhotoAlbumMapper, PhotoAlbumEntity> implements PhotoAlbumService {

    @Autowired
    OssFeignService ossFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<PhotoAlbumEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        System.out.println(params);
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("photo_id",key).or()
                    .eq("img_title",key).or()
                    .eq("img_addr", key).or()
                    .eq("creation_time",key).or()
                    .eq("modification_time",key);

        }
        queryWrapper.orderByDesc("photo_id");

        IPage<PhotoAlbumEntity> page = this.page(
                new Query<PhotoAlbumEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void removeAdImageByIds(String[] photoIds) {
        List<String> imgs = baseMapper.selectBatchIds(Arrays.asList(photoIds)).stream().map((item) -> {
            return item.getImgAddr();
        }).collect(Collectors.toList());

        ossFeignService.removeList(imgs);
    }

}